#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 30005;
int T;
int n, m;
int a[maxn];
int x[maxn], y[maxn];
int po[maxn];
int pri[maxn], isp[maxn << 1];
int len = 0;
void init(){
    for(int i = 2;i <= m + m;i++){
	isp[i] = 1;
    }
    for(int i = 2;i <= m + m;i++){
	if(isp[i]){
	    pri[++len] = i;
	}
	for(int j = 1;j <= len && i * pri[j] <= m + m;j++){
	    isp[i * pri[j]] = 0;
	    if(i % pri[j] == 0) break;
	}
    }
    return ;
}
const int maxm = 1e7 + 5;
vector<pair<int, int>> opt[maxm << 1];
int u[maxm], v[maxm], w[maxm]; int top = 0;
bitset<4005> bt[4005], tmp;
int main(){
    freopen("chess.in", "r", stdin);
    freopen("chess.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> T;
    while(T--){
	cin >> n >> m; len = 0; init();
	for(int i = 1;i <= m + m;i++) opt[i].clear();
	for(int i = 1;i <= n;i++){
	    cin >> x[i] >> y[i];
	}
	for(int i = 1;i <= n;i++){
	    cin >> po[i];
	}
	top = 0;
	for(int i = 1;i <= m + m;i++) opt[i].clear();
	for(int i = 1;i <= n;i++){
	    for(int j = i + 1;j <= n;j++){
		opt[abs(x[i] - x[j]) + abs(y[i] - y[j])].emplace_back(i, j);
	    }
	}
	for(int i = 1;i <= m + m;i++){
	    for(auto pfr : opt[i]){
		u[++top] = pfr.first;
		v[top] = pfr.second;
	    }
	}
	ll ans = 0;
	for(int i = 1;i <= n;i++){
	    bt[i].reset();
	}
	for(int i = 1;i <= top;i++){
	    w[i] = abs(x[u[i]] - x[v[i]]) + abs(y[u[i]] - y[v[i]]);
	    if(isp[w[i]] && (w[i] % 20 == 3 || w[i] <= 10)) w[i] = 1;
	    else w[i] = 0;
	    if(w[i]){
		tmp = bt[u[i]] ^ bt[v[i]];
		if(n <= 100){
		    for(int j = 1;j <= n;j++){
			if(j == u[i] || j == v[i]) continue;
			if(tmp[j])
			    ans += po[u[i]] + po[v[i]] + po[j];
			
		    }
		} else ans += 3 * tmp.count();
	    }
	    bt[u[i]][v[i]] = 1; bt[v[i]][u[i]] = 1;
	}
	cout << ans << '\n';
    }
    cout.flush(); return 0;
}
